﻿using System;
using System.IO;
using System.Text;
#if CONSOLE_CLIENT
using System.Diagnostics;
#else
using UnityEngine;
#endif
public class LogProfiler
{
    static int used_ticks = 0;
    static int startTickCount = 0;
    static StreamWriter mStreamWriter = null;
    static string m_Path = "";
    public static void LogProfileLoading(string str)
    {
        if (mStreamWriter == null)
        {
            return;
        }
            used_ticks = Environment.TickCount - startTickCount;
#if !CONSOLE_CLIENT
            WriteLogToFile(DateTime.Now.ToString() + " " + Environment.TickCount.ToString() + " " + Time.realtimeSinceStartup + "              use:" + used_ticks + "              " + "-loading-" + str);
#endif
            startTickCount = Environment.TickCount;
    }

    private static void WriteLogToFile(string line)
    {
        if (mStreamWriter != null)
        {
            lock (mStreamWriter)
            {
                mStreamWriter.WriteLine(line);
            }
        }
    }

    public static void Init(string path)
    {
        m_Path = path + "_time_pro";
        FileStream fs = new FileStream(m_Path, FileMode.OpenOrCreate);
        mStreamWriter = new StreamWriter(fs);
        mStreamWriter.AutoFlush = true;
    }

    private static void writeProfileKey(string key)
    {
        if(string.IsNullOrEmpty(key))
        {
            return;
        }
        if(mStreamWriter == null)
        {
            return;
        }

    }

    static StringBuilder log = new StringBuilder();

    public static void LogProfileLeaving(string str)
    {
        if (mStreamWriter == null)
        {
            return;
        }
        log.Length = 0;
        used_ticks = Environment.TickCount - startTickCount;
#if !CONSOLE_CLIENT
        log.Append(DateTime.Now.ToString());
        log.Append(" ");
        log.Append(Environment.TickCount.ToString());
        log.Append(" ");
        log.Append(Time.realtimeSinceStartup);
        log.Append("              use:");
        log.Append(used_ticks);
        log.Append("  used heap:");
        log.Append(Profiler.GetMonoUsedSize() * 1f / 1024);
        log.Append("  total heap:");
        log.Append(Profiler.GetMonoHeapSize() * 1f / 1024);
        log.Append("  total reserved:");
        log.Append(Profiler.GetTotalReservedMemory() * 1f / 1024);
        log.Append("  total used:");
        log.Append(Profiler.GetTotalAllocatedMemory() * 1f / 1024);
        log.Append("  total unused:");
        log.Append(Profiler.GetTotalUnusedReservedMemory() * 1f / 1024);
        log.Append("              ");
        log.Append("-Leaving-");
        log.Append(str);
        WriteLogToFile(log.ToString());
#endif
        startTickCount = Environment.TickCount;
    }
}

